package com.beverages.messages.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beverages.messages.client.CustomerMessagesClient;
import com.beverages.messages.client.MerchantIndividualClient;
import com.beverages.messages.client.MerchantOrdersClient;
import com.beverages.messages.common.Result;
import com.beverages.messages.entity.CancelOrderReply;
import com.beverages.messages.entity.RefundReply;
import com.beverages.messages.entity.dto.CancelOrderReplyDTO;
import com.beverages.messages.entity.dto.intermediate.CancelOrderRequestDTO;
import com.beverages.messages.entity.intermediate.CancelOrderRequest;
import com.beverages.messages.entity.intermediate.RefundRequest;
import com.beverages.messages.mapper.CancelOrderReplyMapper;
import com.beverages.messages.service.ICancelOrderReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/merchant/messages/reply-for-cancelling-order")
public class CancelOrderReplyController {

    private final RestTemplate restTemplate;
    private static final String VUE_URL = "http://127.0.0.1:9091/send-message/"; //后续需要修改

    @Autowired
    public CancelOrderReplyController(@Qualifier("restTemplateForVue") RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Autowired
    private MerchantOrdersClient merchantOrdersClient;

    @Autowired
    private CustomerMessagesClient customerMessagesClient;

    @Resource
    private ICancelOrderReplyService cancelOrderReplyService;

    @Resource
    private CancelOrderReplyMapper cancelOrderReplyMapper;

    @GetMapping("/id/{replyId}")
    public Result<CancelOrderReply> getCancelOrderReplyById(@PathVariable("replyId") Integer replyId) {
        return Result.success(cancelOrderReplyService.getById(replyId));
    }

    @GetMapping("/{replyId}")
    public Result<CancelOrderReplyDTO> getCancelOrderReplyDTOById(@PathVariable("replyId") Integer replyId){
        return Result.success(cancelOrderReplyService.convertCancelOrderReplyEntityToCancelOrderReplyDTO(cancelOrderReplyService.getById(replyId)));
    }

    @GetMapping("/page")
    public Result<IPage<CancelOrderRequestDTO>> findCancelOrderReplyPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(defaultValue = "") String orderNumber,
            @RequestParam(defaultValue = "true") Boolean whetherOnlyShowUnreplied) {
        return Result.success(customerMessagesClient.pageShowCustomerCancelOrderRequest(pageNum, pageSize, orderNumber, whetherOnlyShowUnreplied).getData());
    }

    @PostMapping
    public Result reply(@RequestBody CancelOrderReply cancelOrderReply) {
        cancelOrderReply.setId(null);
        cancelOrderReply.setReplyTime(new Date());
        cancelOrderReplyMapper.insert(cancelOrderReply);
        customerMessagesClient.cancelOrderRequestIsReplied(cancelOrderReply.getRequestId(), cancelOrderReply.getId());
        CancelOrderRequest cancelOrderRequest = customerMessagesClient.getCancelOrderRequestById(cancelOrderReply.getRequestId()).getData();
        merchantOrdersClient.cancelOrder(cancelOrderRequest.getOrderNumber());
        return Result.success();
    }

}